﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Fierce.Core.Aop;
using Fierce.Entity.Basic;
using Fierce.MvcExtend.Controllers;
using Fierce.Utility.Helper;
using Microsoft.AspNetCore.Mvc;

namespace Fierce.Mvc.Areas.Basic.Controllers
{
    [Area("Basic")]
    public class MenuController : WorkController
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Page(string id, string pid)
        {
            BasicMenu model;

            if (string.IsNullOrEmpty(id))
            {
                model = new BasicMenu();

                model.Pid = pid;
                model.Type = "null";
                model.IsEnabled = true;
            }
            else { model = ServiceFactory.BasicMenu.Get(id); }

            return View(model);
        }

        public IActionResult Assign()
        {
            return View();
        }

        public string Grid()
        {
            var list = ServiceFactory.BasicMenu.GetAll();

            var dic = new Dictionary<string, string>();
            dic.Add("Icon", "iconCls");

            return JsonHelper.Serialize(list, turns: dic);
        }

        public string UserGrid()
        {
            var list = ServiceFactory.BasicMenu.GetByUserId(UserID, IsSuper);

            var dic = new Dictionary<string, string>();
            dic.Add("Icon", "iconCls");

            return JsonHelper.Serialize(list, turns: dic);
        }

        public string Tree()
        {
            var list = ServiceFactory.BasicMenu.GetAll();

            var newlist = list.Select(m => new
            {
                id = m.ID,
                text = m.Name,
                iconCls = m.Icon,
                pid = m.Pid
            }).ToList();

            newlist.Insert(0, new
            {
                id = "0",
                text = "无父级",
                iconCls = "icon-application",
                pid = string.Empty
            });

            return JsonHelper.Serialize(newlist);
        }

        public string Save(BasicMenu model)
        {
            return ServiceFactory.BasicMenu.Save(model).Serialize();
        }

        public string Remove(string id)
        {
            return ServiceFactory.BasicMenu.Delete(id).Serialize();
        }

        public string SetOrder(string id, string order, string pid)
        {
            return ServiceFactory.BasicMenu.Order(id, order, pid).Serialize();
        }

        public string EnableOrDisable(string id, bool bit)
        {
            return ServiceFactory.BasicMenu.EnableOrDisable(id, bit).Serialize();
        }

        public string SetAssign(string id, string rows)
        {
            var list = JsonHelper.Deserialize<List<BasicFunction>>(rows);

            string[] functionids = list.Select(m => m.ID).ToArray();

            return ServiceFactory.BasicMenuFunction.SetFunction(id, functionids).Serialize();
        }
    }
}